package com.pipe.connector.actor;

import com.pipe.exception.SessionException;
import com.pipe.protocol.BasePackage;
import com.pipe.protocol.login.OkAndEofPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kiven
 */
public class LoggedInActor {
    private static Logger LOG = LoggerFactory.getLogger(LoggedInActor.class);

    public void handler(BasePackage basePackage) throws SessionException {
        OkAndEofPacket okAndEofPacket = OkAndEofPacket.shortBuild(basePackage.getPayLoadByteBuf());
        if (okAndEofPacket.isOkHead() && basePackage.getPayLoadLen() >= OkAndEofPacket.OK_PACKAGE_LENGTH) {
            LOG.info("[LOGGING] - HandshakePackage received!Login in success!");
            return;
        } else if (okAndEofPacket.isEofHead() && basePackage.getPayLoadLen() < OkAndEofPacket.EOF_PACKAGE_LENGTH) {
            LOG.info("[LOGGING] - EofPacket recieved! handshake failed!");
            throw new SessionException("[LOGGING] - EofPacket recieved! handshake failed!");
        } else {
            LOG.info("[LOGGING] - Package of unknown type recieved! handshake failed!");
            throw new SessionException("[LOGGING] - unknown type of package recieved! handshake failed!");
        }
    }
}
